<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">

      <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单标题" prop="goodsSubject">
        <el-input
          v-model="queryParams.goodsSubject"
          placeholder="请输入订单标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="支付方式" prop="channel">
        <el-select
          v-model="queryParams.channel"
          placeholder="请选择支付方式"
          clearable
          @change="handleQuery"
        >
          <el-option label="支付宝" value="alipay"></el-option>
          <el-option label="微信" value="wx"></el-option>
          <el-option label="积分账户" value="jglh_account"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="订单状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择订单状态"
          clearable
          @change="handleQuery"
        >
          <el-option label="待支付" value="1"></el-option>
          <el-option label="已支付" value="2"></el-option>
          <el-option label="已退款" value="-1"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeT"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="支付时间">
        <el-date-picker
          v-model="daterangePayTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="退款时间">
        <el-date-picker
          v-model="daterangeRefundTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList">
      <el-table-column label="订单Id" align="center" prop="orderId"/>
      <el-table-column label="下单人uid" align="center" prop="uid"/>
      <el-table-column label="订单描述" align="center" prop="body"/>
      <el-table-column label="金额信息" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.fee">订单金额: ¥{{ scope.row.fee / 100 }}</div>
          <div v-if="scope.row.amount">支付金额: ¥{{ scope.row.amount ? scope.row.amount / 100 : 0.00 }}</div>
          <div v-if="scope.row.serverCharge">手续费: ¥{{ scope.row.serverCharge ? scope.row.serverCharge : '-' }}</div>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="channel">
        <template slot-scope="scope">
          <span v-if="scope.row.channel === 'alipay'">支付宝</span>
          <span v-else-if="scope.row.channel === 'wx'">微信</span>
          <span v-else-if="scope.row.channel === 'jglh_account'">积分账户</span>
          <span v-else>{{ scope.row.channel }}</span>
        </template>
      </el-table-column>

      <el-table-column label="支付状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 2">支付成功</span>
          <span v-else-if="scope.row.status === 1">未支付</span>
          <span v-else-if="scope.row.status === -1">已退款</span>
          <span v-else>{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单时间" align="center" prop="t" width="180">
        <template slot-scope="scope">
          <span> {{ parseTime(scope.row.t) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listOrder} from "@/api/user/order"

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户订单表格数据
      orderList: [],
      // 支付时间范围
      daterangePayTime: [],
      // 退款时间范围
      daterangeRefundTime: [],
      // 商品id时间范围
      daterangeT: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        body: null,
        channel: null,
        goodsSubject: null,
        orderId: null,
        payTime: null,
        status: null,
        t: null,
        goodsId: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户订单列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangePayTime && '' != this.daterangePayTime) {
        this.queryParams.params["beginPayTime"] = new Date(this.daterangePayTime[0]).getTime()
        this.queryParams.params["endPayTime"] = new Date(this.daterangePayTime[1]).getTime()
      }
      if (null != this.daterangeT && '' != this.daterangeT) {
        // 将日期字符串转换为时间戳（精确到毫秒）
        this.queryParams.params["beginT"] = new Date(this.daterangeT[0]).getTime()
        this.queryParams.params["endT"] = new Date(this.daterangeT[1]).getTime()
      }

      listOrder(this.queryParams).then(response => {
        if (null != this.daterangeRefundTime && '' != this.daterangeRefundTime) {
          this.queryParams.params["beginRefundTime"] = new Date(this.daterangeRefundTime[0]).getTime()
          this.queryParams.params["endRefundTime"] = new Date(this.daterangeRefundTime[1]).getTime()
        }
      })
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows
        this.total = response.total
        this.loading = false
      })
    }
    ,
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    }
    ,
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePayTime = []
      this.daterangeT = []
      this.daterangeRefundTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    }
    ,
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    }
    ,
    /** 查看详情按钮操作 */
    handleView(row) {
      this.$router.push(`/order/detail/${row.id}`);
    }
  }
}
</script>
